package cn.yuhan.simplediary.diary.service.impl;

import cn.yuhan.simplediary.diary.entity.Diary;
import cn.yuhan.simplediary.diary.mapper.DiaryMapper;
import cn.yuhan.simplediary.diary.service.DiaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class DiaryServiceImpl implements DiaryService {

    private final DiaryMapper diaryMapper;

    @Override
    public void createDiary(Diary diary) {
        diaryMapper.insert(diary);
    }

    @Override
    public List<Diary> getUserDiaries(Long userId) {
        return diaryMapper.selectList(new QueryWrapper<Diary>()
                .eq("user_id", userId)
                .eq("hidden", false));
    }

    @Override
    public List<Diary> getAllDiaries() {
        return diaryMapper.selectList(null);
    }

    @Override
    public void updateDiary(Diary diary) {
        diaryMapper.updateById(diary);
    }

    @Override
    public void deleteDiary(Long diaryId) {
        diaryMapper.deleteById(diaryId);
    }

    @Override
    public void hideDiary(Long diaryId,Boolean hide) {
        Diary diary = diaryMapper.selectById(diaryId);
        if (diary != null) {
            diary.setHidden(hide);
            diaryMapper.updateById(diary);
        }
    }
}